<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :inline="true" :model="queryForm">
        <el-form-item :label="sysBusinessNumber.numberName" prop="numberName">
          <el-input
            v-model="queryForm.numberName"
            :placeholder="sysBusinessNumber.numberName"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">{{
            operateType.query
          }}</el-button>
          <el-button @click="cancelForm">{{ operateType.clear }}</el-button>
        </el-form-item>
      </el-form>
      <div class="base-content-operate">
        <el-button v-rights="'Add'" type="primary" @click="onNew">{{
          operateType.add
        }}</el-button>
      </div>
      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
      >
        <el-table-column
          align="center"
          :label="sysBusinessNumber.serialNumber"
          width="50"
        >
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessNumber.numberName">
          <template slot-scope="scope">
            {{ scope.row.numberName }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessNumber.numberType">
          <template slot-scope="scope">
            {{ acquireNumberTypeName(scope.row.numberType) }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessNumber.numberStatus">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.numberState"
              :active-value="1"
              :inactive-value="0"
              @change="serialNumTypeChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column :label="operateType.action">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                v-rights="'Update'"
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >{{ operateType.redact }}</el-link
              >
              <el-link
                v-rights="'Delete'"
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >{{ operateType.delete }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @changePage="currentChange"
      />
    </div>

    <SysBusinessNumberEdit
      v-if="dialogFormVisible"
      ref="SysBusinessNumberEdit"
      :dialog-form-visible.sync="dialogFormVisible"
      :edit-id.sync="editId"
    />
  </div>
</template>

<script>
import {
  deleteRecord,
  getList,
  update,
} from "../../../api/SysBusiness/SysBusinessNumber.js";
import SysBusinessNumberEdit from "./SysBusinessNumberEdit.vue";
import { getDictList } from "../../../api/System/SystemDict";
export default {
  name: "SysBusinessNumberList",
  components: { SysBusinessNumberEdit },
  data() {
    return {
      editId: "",
      numberTypeList: [],
      loading: true,
      list: [],
      queryForm: {
        numberName: "",
        numberType: 0,
        numberState: 0,
        numberNew: "",
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {
    operateType() {
      return this.$t("ModuleSysBusiness.OperateType") || {};
    },
    sysBusinessNumber() {
      return this.$t("SysBusinessNumber") || {};
    },
  },
  created() {
    // 编号类型
    getDictList({ dictCode: "NumberType" }).then((res) => {
      this.numberTypeList = res.data;
    });

    this.getList();
  },
  methods: {
    // 清空筛选条件
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.getList();
    },
    // 状态按钮改变
    serialNumTypeChange(row) {
      update(row)
        .then((res) => {
          if (res.code === 200) {
            this.getList();
          } else {
            this.getList();
          }
        })
        .catch(() => {
          this.getList();
        });
    },
    // 获取编号类型名称
    acquireNumberTypeName(numberType) {
      for (let i = 0; i < this.numberTypeList.length; i++) {
        var data = JSON.parse(this.numberTypeList[i].value);
        if (Number(data[0].value) === Number(numberType)) {
          return this.numberTypeList[i].name;
        }
      }
    },
    currentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.editId = "";
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.editId = rowData.id;
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.getList();
    },
  },
};
</script>
